#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@NAME          : mcimgview.py
@TIME          : 2024/11/26 22:37:00
@AUTHOR        : chenlip
@VERSION       : 0.0.1
@DESCRIPTION   : 使用minio预览图片的相关操作
'''
# --------------------------引用--------------------------
import os
from .minioact import minioclient
from .utils import Utils as ut
# --------------------------引用--------------------------

def getMCDirTree(mcdir:str, mc=None) -> str:
    '''
        获取minio指定目录的目录树
        :Args:
            - mcdir: minio的目录路径， 例如：images/
        :Return:
            - 目录树的html字符串
    '''
    if mc is None:
        print(__file__, "getMCDirTree import minioact success!")
        mc = minioclient()

    _dirdict = mc.list_obj_tree(mcdir)
    _html = ut.dict2ul(_dirdict)

    return _html

def getPicList(mcdir:str, mc=None) -> list:
    '''
        获取minio指定目录的图片列表
        :Args:
            - mcdir: minio的目录路径， 例如：images/bigpic/
        :Return:
            - 图片列表
            [
                {
                    "refname": "mcimg1001",
                    "srcfile": "微软黑体.jpg",
                    "encodename": "微软黑体.jpg",
                    "type": "opentype",
                    "size": 2800,
                    "ctime": "2019-10-09",
                    "url": "http://"
                },
                {
                    "refname": "mcimg1002",
                    "srcfile": "微软黑体.ttf",
                    "encodename": "微软黑体.ttf",
                    "type": "truetype",
                    "size": 2800,
                    "ctime": "2019-10-09",
                    "url": "http://"
                }
            ]
    '''
    _piclist = []
    pictype = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico"]
    if mc is None:
        print(__file__, "getPicList() import minioact success!")
        mc = minioclient()

    _objurl = mc.mc_bk_url + mcdir
    
    _mcpiclist = mc.list_obj(mcdir, pictype)

    i=1000
    for _f in _mcpiclist:
        i += 1
        _meta = mc.getObjMeta(_f)
        _meta["refname"] = "mcimg" + str(i)
        _meta["url"] = _objurl + _meta["encodename"]
        _piclist.append(_meta)

    return _piclist

def getMvList(mcdir:str, mc=None) -> list:
    '''
        获取minio指定目录的图片列表
        :Args:
            - mcdir: minio的目录路径， 例如：video/
        :Return:
            - 图片列表
            [
                {
                    "refname": "mcmv1001",
                    "srcfile": "1001.mp4",
                    "encodename": "1001.mp4",
                    "type": "mp4",
                    "size": 2800,
                    "ctime": "2019-10-09",
                    "url": "http://"
                },
                {
                    "refname": "mcmv1002",
                    "srcfile": "1001.mp4",
                    "encodename": "1001.mp4",
                    "type": "mp3",
                    "size": 2800,
                    "ctime": "2019-10-09",
                    "url": "http://"
                }
            ]
    '''
    _mvlist = []
    mvtype = ["mp4", "avi", "mov", "flv", "mkv", "rmvb", "wmv", "mpg", "mpeg", "rm", "asf", "ts", "webm", "swf"]
    if mc is None:
        print(__file__, "getMvList() import minioact success!")
        mc = minioclient()

    _objurl = mc.mc_bk_url + mcdir
    
    _mcpiclist = mc.list_obj(mcdir, mvtype)

    i=1000
    for _f in _mcpiclist:
        i += 1
        _meta = mc.getObjMeta(_f)
        _meta["refname"] = "mcimg" + str(i)
        _meta["url"] = _objurl + _meta["encodename"]
        _mvlist.append(_meta)

    return _mvlist
